package com.quanyan.club.web.admin;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.quanyan.api.APIResponse;
import com.quanyan.club.entity.admin.ClubActivityDetail;
import com.quanyan.club.entity.admin.ClubActivityList;
import com.quanyan.club.entity.admin.ClubActivityListDo;
import com.quanyan.club.entity.admin.ReqClubActivityAdmin;
import com.quanyan.club.pullclub.entity.req.ReqWithdrawSource;
import com.quanyan.club.pullclub.service.PullClubService;
import com.quanyan.club.service.ClubActivityAdminService;
import com.quanyan.club.service.CppService;
import com.quanyan.club.web.BaseController;
import com.quanyan.common.Plugin.PageObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by yue.yuan on 2016/8/19.
 */
@RestController
@RequestMapping("/admin/api/club/activity")
public class ClubActivityAdminController extends BaseController {

    @Autowired
    private ClubActivityAdminService clubActivityAdminService;
    @Autowired
    private CppService cppService;
    @Autowired
    private PullClubService pullClubService;

    @RequestMapping(value = "withdrawSource",  method = RequestMethod.POST)
    public String queryWithdrawSource(@RequestBody ReqWithdrawSource req){
        req.setPageNo(req.getPageNo() + 1);
        return JSONObject.toJSONString(pullClubService.withdrawSource(req),
                SerializerFeature.WriteNullBooleanAsFalse,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty);
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public String selectClubActivityDetail(@RequestBody ReqClubActivityAdmin req)
    {
        PageObj<List<ClubActivityListDo>> result = clubActivityAdminService.queryClubActivityList(req);
        if (null == result)
        {
            return JSONObject.toJSONString(APIResponse.returnFail("查询失败"));
        }
        return JSONObject.toJSONString(APIResponse.returnSuccess(result),
                SerializerFeature.WriteNullBooleanAsFalse,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty);
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public String selectClubActivityDetail(Integer activityId)
    {
        ClubActivityDetail clubActivityDetail =
                clubActivityAdminService.getClubActivityDetail(activityId);
        if (null == clubActivityDetail)
        {
            return JSONObject.toJSONString(APIResponse.returnFail("查询失败"));
        }
        return JSONObject.toJSONString(APIResponse.returnSuccess(clubActivityDetail),
                SerializerFeature.WriteNullBooleanAsFalse,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String updateClubActivityDetail(@RequestBody ClubActivityDetail clubActivityDetail)
    {
        if(clubActivityAdminService.updateClubActivityDetail(clubActivityDetail)){
            return JSONObject.toJSONString(APIResponse.returnSuccess());
        }
        return JSONObject.toJSONString(APIResponse.returnFail("更新失败"));
    }

    @RequestMapping(value = "/listDelete", method = RequestMethod.GET)
    public String queryDeleteClubActivityDetail(Integer pageNum, Integer pageSize)
    {
        PageObj<List<ClubActivityList>> result =
                clubActivityAdminService.queryDeleteActivity(pageNum, pageSize);
        if (null == result)
        {
            return JSONObject.toJSONString(APIResponse.returnFail("查询失败"));
        }
        return JSONObject.toJSONString(APIResponse.returnSuccess(result),
                SerializerFeature.WriteNullBooleanAsFalse,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String deleteClubActivity(Integer activityId)
    {
        Integer count = clubActivityAdminService.deleteActivityByActivityId(activityId);
        if(null != count && count > 0)
        {
            cppService.pushAdminDeleteClubActivity(activityId);
            return JSONObject.toJSONString(APIResponse.returnSuccess());
        }
        return JSONObject.toJSONString(APIResponse.returnFail("删除失败"));
    }

    @RequestMapping(value = "/recommends", method = RequestMethod.GET)
    public String queryRecommendClubActivity(Integer pageNum, Integer pageSize)
    {
        PageObj<List<ClubActivityList>> result =
                clubActivityAdminService.queryRecommendList(pageNum, pageSize);
        if(null == result)
        {
            return JSONObject.toJSONString(APIResponse.returnFail("查询失败"));
        }
        return JSONObject.toJSONString(APIResponse.returnSuccess(result),
                SerializerFeature.WriteNullBooleanAsFalse,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty);
    }

    @RequestMapping(value = "/updateRecommend", method = RequestMethod.GET)
    public String updateRecommendClubActivity(Integer activityId, Integer isRecommend)
    {
        Integer count = clubActivityAdminService.updateRecommend(activityId, isRecommend);

        if(null != count && count > 0)
        {
            return JSONObject.toJSONString(APIResponse.returnSuccess());
        }
        return JSONObject.toJSONString(APIResponse.returnFail("删除失败"));
    }

}
